﻿using MiniFox.Infrastructure;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MiniFox.Data
{

    /// <summary>
    /// 
    /// </summary>
    public class PagedResultSet : ResultSet, IPagedResultSet
    {
        int _pageIndex;
        int _pageSize;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public PagedResultSet(int pageIndex, int pageSize) : base()
        {
            _pageIndex = pageIndex;
            _pageSize = pageSize;
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageCount
        {
            get { return (int)Math.Ceiling((double)RowCount / (double)PageSize); }
        }
        /// <summary>
        /// 页码(从1开始)
        /// </summary>
        public int PageIndex
        {
            get { return _pageIndex; }
        }
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize
        {
            get { return _pageSize; }
        }
        /// <summary>
        /// 起始行数，从0开始
        /// </summary>
        public int StartRowIndex
        {
            get 
            {
                return (this.PageIndex - 1) * this.PageSize; 
            }
        }
        /// <summary>
        /// 结束行数
        /// </summary>
        public int EndRowIndex
        {
            get 
            {
                if (this.RowCount == 0)
                    return 0;
                if (this.PageIndex < this.PageCount)
                    return this.StartRowIndex + this.PageSize - 1;

                return this.RowCount - 1;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <returns></returns>
        protected override ResultObject<TResult> CreateResultObject<TResult>()
        {
            return new PagedResultObject<TResult>(this.RowCount, this.PageIndex, this.PageSize);
        }
    }

}
